package com.mutouren.web.frame;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.model.Function;

public class OrgUtils {
	
/*	public static String convertOrganizationToJSON(List<Organization> listDept) {
		StringBuilder result = new StringBuilder();	
		boolean first = true;
		result.append("[");
		for(int i = 0; i < listDept.size(); i++) {			
			String temp = convertOrganizationtToJSON(listDept.get(i));
			if(StringUtils.isBlank(temp)) continue;
					
			if(first) {
				result.append(temp);
				first = false;
			} else {
				result.append(",\r\n" + temp);
			}
		}
		result.append("]");
		return result.toString();
	}
	
	private static String convertOrganizationtToJSON(Organization organization) {
		String jsonFormat1 = "{id:%d, pId:%d, name:\"%s\", open:true, mtrUrl:\"%s\"}";	
		if(organization.isRoot()) {
			return String.format(jsonFormat1, organization.getOrgId(), -1, organization.getOrgName(), "");
		} else
		{
			//return String.format(jsonFormat1, organization.getOrgId(), organization.getParentId(), organization.getOrgName(), "");
			return String.format(jsonFormat1, organization.getOrgId(), organization.getParentId(), organization.getOrgNameWithFlag(), "");
		}
	}*/
	
	
/*	public static String convertFunctionToJSON(List<Function> listFunc) {
		return convertFunctionToJSON(listFunc, null, false);
	}*/
	
/*	public static String convertFunctionToJSON(List<Function> listFunc, List<Function> listCheckFunc, boolean isOnlyCheck) {
		Set<Integer> setCheckFunctionId = getFunctionIdSet(listCheckFunc);
		return convertFunctionToJSON2(listFunc, setCheckFunctionId, isOnlyCheck);
	}*/
	
	public static String convertFunctionToJSON2(List<Function> listFunc, Set<Integer> setCheckFunctionId, boolean isOnlyCheck) {
		StringBuilder result = new StringBuilder();	
		//Set<Integer> setId = getFunctionIdSet(listCheckFunc);
		boolean first = true;
		result.append("[");
		for(int i = 0; i < listFunc.size(); i++) {			
			String temp = convertFunctionToJSON(listFunc.get(i), setCheckFunctionId, isOnlyCheck);
			if(StringUtils.isBlank(temp)) continue;
					
			if(first) {
				result.append(temp);
				first = false;
			} else {
				result.append(",\r\n" + temp);
			}
		}
		result.append("]");
		return result.toString();
	}
	
	private static String convertFunctionToJSON(Function func, Set<Integer> setId, boolean isOnlyCheck) {
		String jsonFormat1 = "{id:%d, pId:%d, name:\"%s\", open:true, mtrUrl:\"%s\"}";
		String jsonFormat2 = "{id:%d, pId:%d, name:\"%s\", open:true, mtrUrl:\"%s\", checked:true}";
		if(setId.contains(func.getFuncId())) {
			return String.format(jsonFormat2, func.getFuncId(), func.getParentId(), 
					func.getIsMenu() ? func.getFuncName() : func.getFuncName() + "[不显示]", 
					func.getUrl());
		} else {
			if(isOnlyCheck) {
				return "";
			} else {
				return String.format(jsonFormat1, func.getFuncId(), func.getParentId(), 
						func.getIsMenu() ? func.getFuncName() : func.getFuncName() + "[不显示]",
						func.getUrl());
			}
		}
	}	
	
/*	private static Set<Integer> getFunctionIdSet(List<Function> listFunc) {
		Set<Integer> result = new HashSet<Integer>();
		if (listFunc == null) return result;
		
		for(Function obj :  listFunc) {
			if(!result.contains(obj.getFuncId())) {
				result.add(obj.getFuncId());
			}
		}
			
		return result;
	}*/
	
	public static void doOnlyDisplayProcess(List<Function> listFunc) {
		Set<Integer> noDisplaySet = new HashSet<Integer>();
		for(Function func : listFunc) {
			if(!func.getIsMenu()) {
				noDisplaySet.add(func.getFuncId());
			}
			if(noDisplaySet.contains(func.getParentId())) {
				noDisplaySet.add(func.getFuncId());
			}
		}
		
		for(int i = listFunc.size() - 1; i >= 0; i--) {
			Function func = listFunc.get(i);
			if(noDisplaySet.contains(func.getFuncId())) {
				listFunc.remove(i);
			}
		}	
	}

}
